import type { RouteRecordRaw } from 'vue-router'
import router from '.'
import { localCache } from '@/utils/cache'

const dynamicsRouters: RouteRecordRaw[] = [
  {
    path: '/main/home',
    component: () => import('@/views/main/home/index.vue')
  },

  {
    path: '/main/overview/coretechnology',
    component: () => import('@/views/main/overview/coretechnology.vue')
  },
  {
    path: '/main/overview/goods',
    component: () => import('@/views/main/overview/goods.vue')
  },
  {
    path: '/main/system/user',
    component: () => import('@/views/main/system/user.vue')
  },
  {
    path: '/main/system/role',
    component: () => import('@/views/main/system/role.vue')
  },
  {
    path: '/main/system/menu',
    component: () => import('@/views/main/system/menu.vue')
  },
  {
    path: '/screen',
    component: () => import('@/views/screen/index.vue')
  }
]

export function addRoute() {
  const menus = localCache.get('menus')
  for (const item of menus) {
    const itemRouter = dynamicsRouters.find((i) => i.path === item.path)

    if (itemRouter) {
      if (itemRouter.path.startsWith('/main')) {
        router.addRoute('Main', itemRouter)
      } else {
        router.addRoute(itemRouter)
      }
    }
    if (item.children) {
      for (const childItem of item.children) {
        const addRouter = dynamicsRouters.find((i) => i.path === childItem.path)
        if (addRouter) {
          router.addRoute('Main', addRouter)
        }
      }
    }
  }
}
